# python import
import os

# CMFCore imports
from Products.CMFCore.utils import getToolByName

class ContentActions:
    """A utility, which provides actions for specific content types.
    """
    def MappedImage(self, start_node, current_node, path, name, attrs):
        """
        """
        dirname = os.path.dirname(path)
        path = os.path.join(dirname, attrs["path"])
        
        # Upload image
        image = open(path)
        current_node.setImage(image)
    
        # Set related Object
        catalog = getToolByName(start_node, "portal_catalog")
        brains = catalog.searchResults(
            path = "/".join(start_node.getPhysicalPath()),
            id = attrs["related_object"])
    
        if len(brains) > 0:
            object = brains[0].getObject()
            current_node.setRelatedObject((object,))
            
            
    def Image(self, start_node, current_node, path, name, attrs):
        """
        """
        dirname = os.path.dirname(path)
        path = os.path.join(dirname, attrs["path"])
        
        # Upload image
        image = open(path)
        current_node.setImage(image)